package com.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Description
 * @Author
 * @Date 2020/3/14 19:36
 **/
public class AppProducer {
    private static final String url = "tcp://127.0.0.1:61616";
    private static final String queueuName= "queue-test";

    public static void main(String[] args) throws Exception{

        //1.创建connectionFactory
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);

        //2.创建连接connection
        Connection connection = connectionFactory.createConnection();

        //3.启动连接
        connection.start();

        //4.创建回话
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        //5.创建一个目标
        Destination destination = session.createQueue(queueuName);

        //6.创建生产值
        MessageProducer producer = session.createProducer(destination);

        for (int i = 0; i < 100; i++) {
            //7.创建一个消息
            TextMessage textMessage = session.createTextMessage("test" + i);
            //8.发布消息
            producer.send(textMessage);

            System.out.println("发送消息成功！"+textMessage.getText());
        }

        //9.关闭连接
        connection.close();


    }
}
